<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        .container{
            margin: 50px auto;
            width: 400px;
            background: rgb(130, 121, 121);
        }

        li{
            list-style: none;
        }

        .tabs{
            display: flex;
            border-bottom: 1px solid #000;
        }

        .tab{
            flex: 1px;
            height: 30px;
            text-align: center;
            line-height: 30px;
        }

        .tab.active{
            background: #000;
            color: #fff;
        }

        .content-box section{
            height: 400px;
            display: none;
        }

        .content-box section.active{
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <ul class="tabs">
            <li class="tab active" >标题1</li>
            <li class="tab">标题2</li>
            <li class="tab">标题3</li>
        </ul>

        <div class="content-box">
            <section class="active">标题一对应内容</section>
            <section>标题二对应内容</section>
            <section>标题三对应内容</section>
        </div>
    </div>

    <script>
        const tabs = document.querySelectorAll('.tab')
        const contents = document.querySelectorAll('.content-box section')


        for(let i=0; i<tabs.length; i++){
            tabs[i].onclick = function(){
                // console.log(i);
                for(let i = 0; i<tabs.length; i++){
                    tabs[i].classList.remove('active')
                    contents[i].classList.remove('active')
                }
                this.classList.add('active')
                contents[i].classList.add('active')
            }
        }

        
    </script>
</body>
</html>